﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using PagedList.Mvc;
using ProjectThuCung.Models;

namespace ProjectThuCung.Controllers
{
    public class ALoaiSanPhamController : Controller
    {
        private ThuCungDB db = new ThuCungDB();

        //
        // GET: /ALoaiSanPham/

        //public ActionResult Index()
        //{
        //    return View(db.LoaiSanPham.ToList());
        //}

        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.TenLoaiSanPhamSortParm = String.IsNullOrEmpty(sortOrder) ? "TenLoaiSanPham desc" : "";
            ViewBag.SubLoaiSanPhamSortParm = sortOrder == "SubLoaiSanPham" ? "SubLoaiSanPham desc" : "SubLoaiSanPham";

            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;

            var loaiSanPham = from s in db.LoaiSanPhams
                           select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                loaiSanPham = loaiSanPham.Where(s => s.TenLoaiSanPham.ToUpper().Contains(searchString.ToUpper()));
            }

            switch (sortOrder)
            {
                case "TenLoaiSanPham desc":
                    loaiSanPham = loaiSanPham.OrderByDescending(s => s.TenLoaiSanPham);
                    break;
                case "SubLoaiSanPham":
                    loaiSanPham = loaiSanPham.OrderBy(s => s.SubLoaiSanPham);
                    break;
                case "SubLoaiSanPham desc":
                    loaiSanPham = loaiSanPham.OrderByDescending(s => s.SubLoaiSanPham);
                    break;
                default:
                    loaiSanPham = loaiSanPham.OrderBy(s => s.TenLoaiSanPham);
                    break;
            }
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(loaiSanPham.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /ALoaiSanPham/Details/5

        public ActionResult Details(int id = 0)
        {
            LoaiSanPham loaisanpham = db.LoaiSanPhams.Find(id);
            if (loaisanpham == null)
            {
                return HttpNotFound();
            }

            switch (loaisanpham.SubLoaiSanPham)
            {
                case ProjectThuCung.Models.SubLoaiSP.ThuCung:
                    ViewBag.Sub = "thú cưng";
                    break;
                case ProjectThuCung.Models.SubLoaiSP.Chuong:
                    ViewBag.Sub = "chuồng/lồng";
                    break;
                case ProjectThuCung.Models.SubLoaiSP.Thuoc:
                    ViewBag.Sub = "thuốc";
                    break;
                case ProjectThuCung.Models.SubLoaiSP.ThucAn:
                    ViewBag.Sub = "thức ăn";
                    break;
            }

            //ViewBag.ListSanPham = loaisanpham.GetSanPhamsByLoaiSanPhamID(db);
            
            return View(loaisanpham);
        }

        //
        // GET: /ALoaiSanPham/Create

        public ActionResult Create()
        {
            List<SelectListItem> listSub = new List<SelectListItem>();
            listSub.Add(new SelectListItem() { Selected = true, Text = "thú cưng", Value = ProjectThuCung.Models.SubLoaiSP.ThuCung.ToString() });
            listSub.Add(new SelectListItem() { Selected = false, Text = "chuồng/lồng", Value = ProjectThuCung.Models.SubLoaiSP.Chuong.ToString() });
            listSub.Add(new SelectListItem() { Selected = false, Text = "thuốc", Value = ProjectThuCung.Models.SubLoaiSP.Thuoc.ToString() });
            listSub.Add(new SelectListItem() { Selected = false, Text = "thức ăn", Value = ProjectThuCung.Models.SubLoaiSP.ThucAn.ToString() });

            ViewBag.ListSub = listSub;

            return View();
        }

        //
        // POST: /ALoaiSanPham/Create

        [HttpPost]
        public ActionResult Create(LoaiSanPham loaisanpham)
        {
            if (ModelState.IsValid)
            {
                db.LoaiSanPhams.Add(loaisanpham);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(loaisanpham);
        }

        //
        // GET: /ALoaiSanPham/Edit/5

        public ActionResult Edit(int id = 0)
        {
            LoaiSanPham loaisanpham = db.LoaiSanPhams.Find(id);
            if (loaisanpham == null)
            {
                return HttpNotFound();
            }

            List<SelectListItem> listSub = new List<SelectListItem>();


            switch (loaisanpham.SubLoaiSanPham)
            {
                case ProjectThuCung.Models.SubLoaiSP.ThuCung:
                    listSub.Add(new SelectListItem() { Selected = true, Text = "thú cưng", Value = ProjectThuCung.Models.SubLoaiSP.ThuCung.ToString() });
                    listSub.Add(new SelectListItem() { Selected = false, Text = "chuồng/lồng", Value = ProjectThuCung.Models.SubLoaiSP.Chuong.ToString() });
                    listSub.Add(new SelectListItem() { Selected = false, Text = "thuốc", Value = ProjectThuCung.Models.SubLoaiSP.Thuoc.ToString() });
                    listSub.Add(new SelectListItem() { Selected = false, Text = "thức ăn", Value = ProjectThuCung.Models.SubLoaiSP.ThucAn.ToString() });

                    break;
                case ProjectThuCung.Models.SubLoaiSP.Chuong:
                    listSub.Add(new SelectListItem() { Selected = false, Text = "thú cưng", Value = ProjectThuCung.Models.SubLoaiSP.ThuCung.ToString() });
                    listSub.Add(new SelectListItem() { Selected = true, Text = "chuồng/lồng", Value = ProjectThuCung.Models.SubLoaiSP.Chuong.ToString() });
                    listSub.Add(new SelectListItem() { Selected = false, Text = "thuốc", Value = ProjectThuCung.Models.SubLoaiSP.Thuoc.ToString() });
                    listSub.Add(new SelectListItem() { Selected = false, Text = "thức ăn", Value = ProjectThuCung.Models.SubLoaiSP.ThucAn.ToString() });

                    break;
                case ProjectThuCung.Models.SubLoaiSP.Thuoc:
                    listSub.Add(new SelectListItem() { Selected = false, Text = "thú cưng", Value = ProjectThuCung.Models.SubLoaiSP.ThuCung.ToString() });
                    listSub.Add(new SelectListItem() { Selected = false, Text = "chuồng/lồng", Value = ProjectThuCung.Models.SubLoaiSP.Chuong.ToString() });
                    listSub.Add(new SelectListItem() { Selected = true, Text = "thuốc", Value = ProjectThuCung.Models.SubLoaiSP.Thuoc.ToString() });
                    listSub.Add(new SelectListItem() { Selected = false, Text = "thức ăn", Value = ProjectThuCung.Models.SubLoaiSP.ThucAn.ToString() });

                    break;
                case ProjectThuCung.Models.SubLoaiSP.ThucAn:
                    listSub.Add(new SelectListItem() { Selected = false, Text = "thú cưng", Value = ProjectThuCung.Models.SubLoaiSP.ThuCung.ToString() });
                    listSub.Add(new SelectListItem() { Selected = false, Text = "chuồng/lồng", Value = ProjectThuCung.Models.SubLoaiSP.Chuong.ToString() });
                    listSub.Add(new SelectListItem() { Selected = false, Text = "thuốc", Value = ProjectThuCung.Models.SubLoaiSP.Thuoc.ToString() });
                    listSub.Add(new SelectListItem() { Selected = true, Text = "thức ăn", Value = ProjectThuCung.Models.SubLoaiSP.ThucAn.ToString() });

                    break;
            }

            ViewBag.ListSub = listSub;

            return View(loaisanpham);
        }

        //
        // POST: /ALoaiSanPham/Edit/5

        [HttpPost]
        public ActionResult Edit(LoaiSanPham loaisanpham)
        {
            if (ModelState.IsValid)
            {
                db.Entry(loaisanpham).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(loaisanpham);
        }

        //
        // GET: /ALoaiSanPham/Delete/5

        public ActionResult Delete(int id = 0)
        {
            LoaiSanPham loaisanpham = db.LoaiSanPhams.Find(id);
            if (loaisanpham == null)
            {
                return HttpNotFound();
            }

            switch (loaisanpham.SubLoaiSanPham)
            {
                case ProjectThuCung.Models.SubLoaiSP.ThuCung:
                    ViewBag.Sub = "thú cưng";
                    break;
                case ProjectThuCung.Models.SubLoaiSP.Chuong:
                    ViewBag.Sub = "chuồng/lồng";
                    break;
                case ProjectThuCung.Models.SubLoaiSP.Thuoc:
                    ViewBag.Sub = "thuốc";
                    break;
                case ProjectThuCung.Models.SubLoaiSP.ThucAn:
                    ViewBag.Sub = "thức ăn";
                    break;
            }
            return View(loaisanpham);
        }

        //
        // POST: /ALoaiSanPham/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            LoaiSanPham loaisanpham = db.LoaiSanPhams.Find(id);
            db.LoaiSanPhams.Remove(loaisanpham);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}